// 设置默认信息
$.validator.setDefaults({
    submitHandler: function (val) {
        oktipsoption = {
            type: 1,
            url: $('.formsub').attr('url'),
            msg: '密码设置成功'
        };
        ajaxform.init(oktipsoption, '.formsub');
        ajaxform.submit();
    },
    errorPlacement: function (error, element) {
        element.parents('.l_b').append(error);
        element.parents('.l_b').find('.prompt').hide();
    }
});

$(document).ready(function () {

    $(".l_b input").focus(function () {
        $(this).addClass('u-ipt-active');
        $(this).removeClass('error');
        $(this).next('.error').hide();
    });


    $(".l_b input").blur(function () {
        $(this).removeClass('u-ipt-active');
        if ($(this).parents('.l_b').find('label.error').is(":visible")) {
            $(this).addClass('error');
            $(this).parents('.l_b').find('.prompt').hide();
        } else {
            $(this).removeClass('error');
            $(this).removeClass('u-ipt-active');
            $(this).parent('.file_name').removeClass('error');
             $(this).parents('.l_b').find('.prompt').show();
        }
    });

    // 验证手机号码
    $.validator.addMethod("chkphone", function (value) {
        var phoneReg = /^1\d{10}$/;
        return phoneReg.test(value);
    });

    // 验证短信验证码
    $.validator.addMethod("chkyzcode", function (value) {
        var yzcodeReg = /^\d{6}$/;
        return yzcodeReg.test(value);
    });

    // 验证密码
    $.validator.addMethod("chkpassword", function (value) {
        var pwdReg = /[^\u4e00-\u9fa5]+/;
        return pwdReg.test(value);
    });

    $("#phone_form").validate({
        rules: {
            phone: {
                required: true,
                chkphone: true,
                remote: {
                    url: $("#checkphone").val(),
                    type: "Post",
                    async: false,
                    data: {
                        phone: function () {
                            return $("#phone").val();
                        }
                    },
                    dataType: "json"
                }
            },
            yzcode: {
                required: true,
                chkyzcode: true,
                remote: {
                    url: $("#checkphonecode").val(),
                    type: "Post",
                    async: false,
                    data: {
                        yzcode: function () {
                            return $("#yzcode").val();
                        }
                    },
                    dataType: "json"
                }
                // yzcodeck: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 12,
                chkpassword: true
            },
            repassword: {
                required: true,
                equalTo: "#password"
            }
        },
        messages: {
            phone: {
                required: '请输入11位手机号码',
                chkphone: '手机号码不正确',
                remote: '手机号码不存在'
            },
            yzcode: {
                required: '请输入短信验证码',
                chkyzcode: '验证码为6位数字',
                remote: '请输入正确的短信验证码'
                        // yzcodeck:'请输入正确的短信验证码'
            },
            password: {
                required: '请输入密码',
                minlength: '至少输入6位非中文密码',
                maxlength: '最多12位非中文密码',
                chkpassword: '请输入6-12位非中文密码'
            },
            repassword: {
                required: '请再次输入密码',
                equalTo: "两次密码输入不一致"
            }
        },
        focusInvalid: false,
        onkeyup: false
    });
});

// 短信验证计时
var InterValObj1; //timer变量，控制时间
var curCount1;//当前剩余秒数
function sendMessage() {
    var count = 60; //间隔函数，1秒执行
    if ($("#phone_form").validate().element($("#phone"))) {
        curCount1 = count;
        //设置button效果，开始计时
        $(".js_yzcode").attr("disabled", "true");
        $(".js_yzcode").text(curCount1 + "S");
        $(".js_yzcode").css("background", "#ccc");
        $(".js_yzcode").removeAttr("onclick",'');
        InterValObj1 = setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        //向后台发送处理数据s
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "json", //数据格式:JSON
            url: $('#sendphonecode').val(), //目标地址
            data: {phone: $("#phone").val()},
        });
    }
}

//timer处理函数
function SetRemainTime() {
    if (curCount1 == 0) {
        clearInterval(InterValObj1);//停止计时器
        $(".js_yzcode").removeAttr("disabled");//启用按钮
        $(".js_yzcode").text("重新发送");
        $(".js_yzcode").css("background", "#f60");
         $(".js_yzcode").attr("onclick",'sendMessage()');
    } else {
        curCount1--;
        $(".js_yzcode").text(curCount1 + "S");
    }
}